package y23.Dec;

public class Test3 {
    public static void main(String[] args) {
        System.out.println(new Test3().mergeAlternately("aaa", "bbb"));
        System.out.println(new Test3().mergeAlternately("aaaaaaaa", "bbb"));
        System.out.println(new Test3().mergeAlternately("aaa", "bbbbbbb"));
        System.out.println(new Test3().mergeAlternately("", "bbb"));
        System.out.println(new Test3().mergeAlternately("aaa", ""));
        System.out.println(new Test3().mergeAlternately("",""));
    }
    public String mergeAlternately(String word1, String word2) {
        if(word1.length() == 0){
            return word2;
        }
        if(word2.length() == 0){
            return word1;
        }
        StringBuffer str = new StringBuffer();
        if (word1.length() == word2.length()){
            str.append(append(word1,word2,word1.length()));
        }else if(word1.length() > word2.length()){
            str.append(append(word1,word2,word2.length()));
            str.append(word1.substring(word2.length()));
        }else {
            str.append(append(word1,word2,word1.length()));
            str.append(word2.substring(word1.length()));
        }
        return str.toString();
    }
    public String append(String word1,String word2,int l){
        StringBuffer  str = new StringBuffer();
        for(int i = 0;i < l;i++){
            str.append(word1.charAt(i));
            str.append(word2.charAt(i));
        }
        return str.toString();
    }
}
